import datetime
import logging
import os
import time
import wsgiref.handlers
import random

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import py.datamodel

class Admin(webapp.RequestHandler):
    def get(self):
        self.post()
        
    def post(self):
        self.response.headers['Content-Type'] = 'text/html'
        ownerId = self.request.get("ownerId")
        view = self.request.get("view")
       
        template_values = {}
        template_values["view"] = view
        template_values["ownerId"] = ownerId

        query = db.Query(py.datamodel.Visit)
        query.filter("ownerId =", ownerId)
        if query.count() == 0:
            self.response.out.write("No user with user Id " + ownerId)
            return

        visitRecord = query.get()
        template_values["owner"] = visitRecord.owner

        path =  os.path.join(os.path.dirname(__file__), '../templates/admin.html')
        path = os.path.abspath(path);
        self.response.out.write(template.render(path, template_values))
        
        
if __name__ == '__main__':
  application = webapp.WSGIApplication(
      [
        ('/admin', Admin)
      ],
      debug=True)

  wsgiref.handlers.CGIHandler().run(application)
